#!/usr/bin/env escript
%% -*- erlang -*-
%%! -config blog.config

%% Main entry point.
main(_) ->
  setup_codepath(),
  start_apps(),

  % Install our event manager and register our handlers (not mandatory).
  {ok, _EventManagerPid} = gen_event:start({local, my_event_bus}),
  gen_event:add_handler(my_event_bus, blog_event_handler, []),

  % Let's first create our db schema. Database should be already created. All
  % other schemas are auto generated.
  ok = sumo:create_schema(post),
  ok = sumo:create_schema(author),
  ok = sumo:create_schema(vote),
  ok = sumo:create_schema(reader),

  % Cleanup authors and posts, so we can have a fresh runup.
  _NumRows1 = blog:del_author(),
  _NumRows2 = blog:del_post(),
  _NumRows3 = blog:del_reader(),

  % Let's create a new blog author and a new blog entry. The result is the
  % same author and post with their new id's filled in.
  Author = blog:new_author(<<"Pepe Gorostiga">>, <<"some binary jpg">>),
  Post = blog:new_post("How awesome sumo_db is", "It just make things easy", Author),

  % Let's now delete them.
  true = blog:del_post(Post),
  true = blog:del_author(Author),

  % Let's create them again
  Author2 = blog:new_author(<<"Pepe Gorostiga">>, <<"some binary jpg">>),
  _Author3 = blog:new_author(<<"Some other guy">>, <<"some other photo">>),
  Post2 = blog:new_post("How awesome sumo_db is", "It just make things easy", Author2),

  % Update author and post.
  blog:save_author(blog_author:update_photo(<<"new photo">>, Author2)),
  blog:save_post(blog_post:update_title("My new shiny title", Post2)),

  io:format("Authors by name: ~p~n", [blog:find_authors_by_name(<<"Pepe Gorostiga">>, 10, 0)]),
  io:format("Authors: ~p~n", [blog:find_all_authors(10, 0)]),
  io:format("A blog: ~p~n", [blog:find_post(blog_post:id(Post2))]),
  io:format("An author: ~p~n", [blog:find_author(blog_author:id(Author2))]),

  % Call a specific method of a store (not handled by the generic store,
  % i.e: a "complex" work).
  % io:format("Total number of posts: ~p~n", [blog:total_posts()]),

  % Create a blog reader, in a nother store and backend.
  Reader = blog:new_reader("Marcelo Gornstein", "marcelo@inakanetworks.com"),
  Reader2 = blog:new_reader("Pepe Gorostiga", "pepe@asd.com"),
  io:format("A Reader: ~p~n", [blog:find_reader(blog_reader:id(Reader))]),

  blog:save_reader(blog_reader:update_email("asd@asd.com", Reader)),
  blog:save_reader(blog_reader:update_email("asd@asd.com", Reader2)),
  io:format("Another Reader: ~p~n", [blog:find_reader(blog_reader:id(Reader))]),

  % Create a vote
  _Vote = blog:new_vote(blog_reader:id(Reader2), blog_post:id(Post2)),

  io:format("Deleted ~p authors~n", [blog:del_author_by_name(<<"Pepe Gorostiga">>)]).

setup_codepath() ->
  Dir = filename:absname(""),
  Deps = [
    "blog", "goldrush", "worker_pool", "lager", "quickrand",
    "sumo_db", "uuid_erl"
  ],
  DepsDir = filename:absname(Dir ++ "/" ++ "_build/default/lib"),
  [ code:add_patha(DepsDir ++ "/" ++ X ++ "/ebin") || X <- Deps].

start_apps() ->
  application:ensure_all_started(blog).
